package com.work.libtest2.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.work.libtest2.dto.ExtraWorkDto;
import com.work.libtest2.entity.ExtraWork;
import com.work.libtest2.entity.R;
import com.work.libtest2.service.ExtraWorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: ExtraWorkController
 * Package: com.work.libtest2.controller
 * Description:
 *
 * @Author
 * @Create 2024/6/10 15:32
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/extraWork")
public class ExtraWorkController {

    @Resource
    private ExtraWorkService extraWorkService;

    @GetMapping("{currentPage}/{pageSize}")
    public R getPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize, ExtraWorkDto extraWorkDto){
        IPage<ExtraWorkDto> page = extraWorkService.getPage(currentPage,pageSize,extraWorkDto);
        if (currentPage>page.getPages()){
            page = extraWorkService.getPage((int)page.getPages(),pageSize,extraWorkDto);
        }
        return new R(true,page);
    }

    @GetMapping("/getAllEmpName")
    public R getAllEmpName(){
      List<ExtraWork> extraWorks =  extraWorkService.getAllEmpName();
      return new R(true,extraWorks);
    }
    /**
     * 添加加班
     * */
    @PostMapping
    public R addExtraWork(@RequestBody ExtraWork extraWork){
       int i = extraWorkService.addExtraWork(extraWork);
       return new R(i>0,i>0?"添加成功":"添加失败");
    }
    @GetMapping("{ewId}")
    public R getById(@PathVariable Integer ewId){
      ExtraWork extraWork =  extraWorkService.getById(ewId);
      return new R(true,extraWork);
    }
    @PutMapping
    public R updateExtraWork(@RequestBody ExtraWork extraWork){
        int i =extraWorkService.updateExtraWork(extraWork);
        return new R(i>0);
    }
    @DeleteMapping("{ewId}")
    public R deleteById(@PathVariable Integer ewId){
       int i = extraWorkService.deleteById(ewId);
       return new R(i>0);
    }
}
